﻿package  common.components.transition.transitions.mosaic
{
	import common.components.transition.transitions.Transition;
	import common.components.transition.transitions.TransitionRunner;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import mx.controls.Image;
	import mx.core.UIComponent;
	import mx.events.EffectEvent;
	
	
	public class Mosaic extends Transition 
	{
		protected var mask:Loader;
		
		protected var maskId:int;
		
		public function Mosaic(runner:TransitionRunner,image:Image,transTime:Number,stayTime:Number) 
		{
			super(runner,image,transTime,stayTime);
		}
	
		override public function walkIn():void
		{
//            var context:LoaderContext
//            context = new LoaderContext();
			this.mask = new Loader();
			this.mask.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onMaskLoadComplete);
            this.mask.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onMaskLoadFailed);
            //this.mask.load(new URLRequest('http://www.aipai.com/app/www/player/transitions/'+ this.maskId + ".swf" + "?x=" + Math.random()));
            this.mask.load(new URLRequest('http://www.aipai.com/app/www/player/transitions/'+ this.maskId + ".swf"));
		}
		
		private function onMaskLoadComplete(evt:Event) : void
		{
			this.mask.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.onMaskLoadComplete);
            this.mask.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, this.onMaskLoadFailed);
			super.walkIn();
		}
	
		override protected function transInStart() : void
		{
			super.transInStart();
//			this.image.mask = this.mask;
			var ui:UIComponent = new UIComponent;
			ui.width = this.image.parent.width;
			ui.height = this.image.parent.height;
			ui.addChild(this.mask);
			this.image.addChild(ui);
		}
		
		private function onMaskLoadFailed() : void
		{
			this.mask.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.onMaskLoadComplete);
        	this.mask.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, this.onMaskLoadFailed);
		}
		
		override protected function onTransOutEnd(evt:EffectEvent):void
		{
			this.mask.parent.removeChild(this.mask);
						
			super.onTransOutEnd(evt);
		}	
		
	}
}